<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/04/11 20:58:15 $)
// $RCSfile: livesupport.php,v $ : $Revision: 1.24 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

define("CHAT_INCOMING", 1);
define("CHAT_INCHAT", 2);
define("CHAT_ENDED", 3);
define("CHAT_NOANSWER", 4);
define("CHAT_TIMEOUT", 5);
define("CHAT_STAFF", 6);
define("CHAT_CLIENT", 7);

define("MESSAGE_CLIENT", 1);
define("MESSAGE_STAFF", 2);
define("MESSAGE_NEW", 3);
define("MESSAGE_REPLIED", 4);
define("CONTENT_STAFF", 5);
define("CONTENT_CLIENT", 6);

define("ACTION_MSG", "message");
define("ACTION_URL", "url");
define("ACTION_IMAGE", "image");
define("ACTION_CODE", "code");
define("ACTION_CHATLEAVE", "leave");
define("ACTION_CHATENTER", "enter");
define("ACTION_TYPING", "typing");
define("ACTION_VOICECHAT", "voicechat");
define("ACTION_VIEWSHARE", "viewshare");

define("TRANSFER_PENDING", 1);
define("TRANSFER_ACCEPTED", 2);

define("PROACTIVE_NONE", 0);
define("PROACTIVE_REQUEST", 4);
define("PROACTIVE_FORCED", 6);

define("ENTRY_TEXT", "text");
define("ENTRY_CODE", "code");
define("ENTRY_URL", "url");
define("ENTRY_IMAGE", "image");

define("SUBMIT_STAFF", 1);
define("SUBMIT_CLIENT", 2);

define("STATE_BROWSING", 1);
define("STATE_PENDINGCHAT", 2);
define("STATE_INCHAT", 3);

define("VOIP_PENDING", 1);
define("VOIP_STARTED", 2);

/**
* Live Support Module
*/
class livesupport {

	/**
	* Constructor to register any events, sections
	*/
	function livesupport() {
		global $_SWIFT, $events, $datastore, $departments, $template;
		
		$events->registerEventModuleSWIFT(MODULE_LIVESUPPORT, $this);

		// ======= VISITOR =======
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "htmlcode");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "updatefootprint");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "startclientchat");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "chatheaderframe");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "chatfooterframe");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "chatstartcontentframe");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "messagecontentframe");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "chatcss");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "leaveamessage");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "startformchat");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "clientchatpostframe");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "clientchatloopframe");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "chatstartedcontentframe");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "submitclientchatmsg");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "endvisitorchat");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "resetproactivestatus");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "emailchat");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "emailimage");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "visitortyping");
		$events->registerEvent(EVENT_VISITOR, MODULE_LIVESUPPORT, "track");

		// ======= WINAPP =======
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "fetchvisitors");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "visitorhistory");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "blockvisitor");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "acceptchat");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "startstaffchat");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "chatloopframe");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "chatloopxml");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "postchatmessage");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "refusechat");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "staffaddnote");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "visitornotes");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "usertyping");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "chatendsession");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "visitorchatstatus");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "staffpushcode");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "pushpagevisitor");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "pushimagevisitor");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "emailchat");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "transferchat");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "proactiveforcedchat");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "proactiverequestchat");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "startstafftostaffchat");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "startvoicechat");
		$events->registerEvent(EVENT_WINAPP, MODULE_LIVESUPPORT, "startviewshare");

		// ======= STAFF CP =======
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "chathistory");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "viewchat");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "messages");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "repliedmessages");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "replytomessage");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "messagedetails");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "canned");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "managecannedresponses");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "insertcannedcategory");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "insertcannedresponse");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "delcannedcategory");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "editcannedcategory");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "editcannedresponse");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "htmlbutton");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "emailbutton");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "textlink");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "adtracking");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "visitortracking");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "insertcampaign");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "adreports");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "editcampaign");
		$events->registerEvent(EVENT_STAFF, MODULE_LIVESUPPORT, "ajax");

		// ======= MONITOR =======
		$events->registerEvent(EVENT_MONITOR, MODULE_LIVESUPPORT, "monitordashboard");
		$events->registerEvent(EVENT_MONITOR, MODULE_LIVESUPPORT, "monitorloop");

		// ======= ADMIN CP =======
		$events->registerEvent(EVENT_ADMIN, MODULE_LIVESUPPORT, "visitorbans");
		$events->registerEvent(EVENT_ADMIN, MODULE_LIVESUPPORT, "editvisitorban");

		// ======= CALLBACK =======
		$events->registerEvent(EVENT_CALLBACK, MODULE_LIVESUPPORT, "winappLogin");
		$events->registerEvent(EVENT_CALLBACK, MODULE_LIVESUPPORT, "winappLogout");
		
		// ======= CODE TO LOAD PRE CALLBACK DATA STORES =======
		$eventaction = $events->eventdata["eventaction"];
		if ($eventaction == "fetchvisitors" || $eventaction == "monitordashboard" || $eventaction == "monitorloop")
		{
			$datastore->cache("livesupportcache");
			loadStaffCaches();
		} else if ($eventaction == "startformchat" || $eventaction == "startclientchat") {
			require_once ("./includes/functions_departments.php");
			$departments = new Departments;

			loadStaffCaches();
		} else if ($eventaction == "clientchatloopframe" || $eventaction == "chatstartedcontentframe" || $eventaction == "refusechat" || $eventaction == "visitorbans") {
			loadStaffCaches();
		} else if ($eventaction == "blockvisitor" || $eventaction == "visitornotes" || $eventaction == "postchatmessage" || $eventaction == "usertyping" || $eventaction == "chatendsession" || $eventaction == "visitorchatstatus" || $eventaction == "staffpushcode" || $eventaction == "pushpagevisitor" || $eventaction == "pushimagevisitor" || $eventaction == "emailchat" || $eventaction == "transferchat" || $eventaction == "visitorhistory" || $eventaction == "proactiveforcedchat" || $eventaction == "proactiverequestchat" || $eventaction == "startstaffchat" || $eventaction == "startstafftostaffchat" || $eventaction == "messages" || $eventaction == "replytomessage" || $eventaction == "repliedmessages" || $eventaction == "messagedetails" || $eventaction == "canned" || $eventaction == "htmlbutton" || $eventaction == "emailbutton" || $eventaction == "textlink" || $eventaction == "chathistory" || $eventaction == "viewchat" || $eventaction == "adtracking" || $eventaction == "insertcampaign" || $eventaction == "adreports" || $eventaction == "startvoicechat" || $eventaction == "adtracking" || $eventaction == "track" || $eventaction == "visitortracking") {
			loadStaffCaches();
			$datastore->cache("campaigncache");
			$template->loadLanguageSection("livesupport");
		}
	}

	/**
	* Destructor, called to unregister any events etc
	*/
	function _livesupport() {
	}

	/*
	* Call back function
	*/
	function _eventcallback($eventtype, $eventaction)
	{
		global $_SWIFT, $interface, $template, $departments, $staffauth, $module, $grid, $xml, $staff, $settings, $dbCore, $events, $session, $loginshare, $cookie, $errormessage, $infomessage, $permissions, $mimelist;

		if ($eventtype == EVENT_VISITOR && $eventaction == "htmlcode")
		{
			require_once ("./modules/livesupport/visitor_htmlcode.php");

			return true;			
		} else if ($eventtype == EVENT_VISITOR && $eventaction == "updatefootprint") {
			require_once ("./modules/livesupport/visitor_updatefootprint.php");

			return true;
		} else if ($eventtype == EVENT_VISITOR && ($eventaction == "emailchat" || $eventaction == "emailimage")) {
			require_once ("./modules/livesupport/visitor_emailimage.php");

			return true;
		} else if ($eventtype == EVENT_VISITOR && ($eventaction == "startclientchat" || $eventaction == "chatheaderframe" || $eventaction == "chatfooterframe" || $eventaction == "chatstartcontentframe" || $eventaction == "chatcss" || $eventaction == "clientchatpostframe" || $eventaction == "chatstartedcontentframe" || $eventaction == "startformchat" || $eventaction == "submitclientchatmsg" || $eventaction == "endvisitorchat" || $eventaction == "visitortyping")) {
			require_once ("./modules/livesupport/client_chat.php");

			return true;
		} else if ($eventtype == EVENT_VISITOR && $eventaction == "clientchatloopframe") {
			require_once ("./modules/livesupport/client_chatloop.php");

			return true;
		} else if ($eventtype == EVENT_VISITOR && ($eventaction == "messagecontentframe" || $eventaction == "leaveamessage")) {
			require_once ("./modules/livesupport/client_message.php");

			return true;
		} else if ($eventtype == EVENT_VISITOR && ($eventaction == "resetproactivestatus")) {
			$template->loadLanguageSection("livesupportclient");
			require_once ("./modules/livesupport/client_misc.php");

			return true;
		} else if ($eventtype == EVENT_WINAPP && $eventaction == "fetchvisitors") {
			require_once ("./modules/livesupport/winapp_fetchvisitors.php");

			return true;
		} else if ($eventtype == EVENT_WINAPP && ($eventaction == "blockvisitor"  || $eventaction == "staffaddnote"  || $eventaction == "visitornotes" || $eventaction == "visitorchatstatus" || $eventaction == "visitorhistory")) {
			require_once ("./modules/livesupport/winapp_misc.php");

			return true;
		} else if ($eventtype == EVENT_WINAPP && ($eventaction == "acceptchat" || $eventaction == "startstaffchat" || $eventaction == "postchatmessage" || $eventaction == "refusechat" || $eventaction == "usertyping" || $eventaction == "chatendsession" || $eventaction == "staffpushcode" || $eventaction == "pushpagevisitor" || $eventaction == "pushimagevisitor" || $eventaction == "emailchat" || $eventaction == "transferchat" || $eventaction == "proactiveforcedchat" || $eventaction == "proactiverequestchat" || $eventaction == "startstafftostaffchat" || $eventaction == "startvoicechat" || $eventaction == "startviewshare")) {
			require_once ("./modules/livesupport/winapp_chat.php");

			return true;
		} else if ($eventtype == EVENT_WINAPP && $eventaction == "chatloopframe") {
			require_once ("./modules/livesupport/winapp_chatloop.php");

			return true;
		} else if($eventtype == EVENT_WINAPP && $eventaction == "chatloopxml") {
			require_once ("./modules/livesupport/winapp_chatloopxml.php");
			
			return true;
		}else if ($eventtype == EVENT_STAFF && ($eventaction == "chathistory" || $eventaction == "viewchat")) {
			require_once ("./modules/livesupport/staff_chathistory.php");

			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "ajax")) {
			require_once ("./modules/livesupport/staff_ajax.php");

			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "messages" || $eventaction == "repliedmessages" || $eventaction == "replytomessage" || $eventaction == "messagedetails")) {
			require_once ("./modules/livesupport/staff_messages.php");

			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "canned" || $eventaction == "insertcannedcategory" || $eventaction == "managecannedresponses" || $eventaction == "insertcannedresponse" || $eventaction == "editcannedcategory" || $eventaction == "editcannedresponse")) {
			require_once ("./modules/livesupport/staff_canned.php");

			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "htmlbutton" || $eventaction == "emailbutton" || $eventaction == "textlink" || $eventaction == "visitortracking")) {
			require_once ("./modules/livesupport/staff_tags.php");

			return true;
		} else if ($eventtype == EVENT_CALLBACK && ($eventaction == "winappLogin" || $eventaction == "winappLogout")) {
			require_once ("./modules/livesupport/winapp_initexit.php");

			return true;
		} else if ($eventtype == EVENT_STAFF && ($eventaction == "adtracking" || $eventaction == "insertcampaign" || $eventaction == "adreports" || $eventaction == "editcampaign")) {
			require_once ("./modules/livesupport/staff_adtracking.php");

			return true;
		} else if ($eventtype == EVENT_VISITOR && ($eventaction == "track")) {
			require_once ("./modules/livesupport/visitor_adtracking.php");

			return true;
		} else if ($eventtype == EVENT_MONITOR && ($eventaction == "monitordashboard")) {
			require_once ("./modules/livesupport/monitor_main.php");

			return true;
		} else if ($eventtype == EVENT_MONITOR && ($eventaction == "monitorloop")) {
			require_once ("./modules/livesupport/monitor_loop.php");

			return true;
		} else if ($eventtype == EVENT_ADMIN && ($eventaction == "visitorbans" || $eventaction == "editvisitorban")) {
			require_once ("./modules/livesupport/admin_visitorbans.php");

			return true;
		}

		return false;
	}
}
?>